﻿options nolabel;
proc import out=work.test3 datafile="I:/prepare/pseudo_data.dta" dbms=dta replace;run;
proc import out=work.crypto_factor datafile="I:/prepare/crypto_factor.dta" dbms=dta replace;run;  
data umd; set test3;if year=2019 or (year=2018 and month>=8) or year=2020 or  year=2021;
 proc means data=test16; run;
proc sort data=umd; by date; run;
proc rank data=umd out=umd2 group=5;
  by date;
    var ab_turn_daily_new_30;
    ranks momr;
run;

proc sort data=umd2 ; by date momr ; run;
  
data umd3;
set umd2 ;
where momr>=0;
momr=momr+1;
HDATE1 = intnx("day",date, 1,"B");
HDATE2 = intnx("day",date,2,"B");
HDATE3 = intnx("day",date,3,"B");
HDATE4 = intnx("day",date,4,"B");
HDATE5 = intnx("day",date,5,"B");
HDATE6 = intnx("day",date,6,"B");
HDATE7 = intnx("day",date,7,"B");
HDATE8 = intnx("day",date,8,"B");
HDATE9 = intnx("day",date,9,"B");
HDATE10 = intnx("day",date,10,"B");
HDATE11 = intnx("day",date,11,"B");
HDATE12 = intnx("day",date,12,"B");
HDATE13 = intnx("day",date,13,"B");
HDATE14= intnx("day",date,14,"B");
HDATE15 = intnx("day",date,15,"B");
HDATE16 = intnx("day",date,16,"B");
HDATE17 = intnx("day",date,17,"B");
HDATE18 = intnx("day",date,18,"B");
HDATE19 = intnx("day",date,19,"B");
HDATE20 = intnx("day",date,20,"B");
HDATE21 = intnx("day",date,21,"B");
HDATE22 = intnx("day",date,22,"B");
HDATE23 = intnx("day",date,23,"B");
HDATE24 = intnx("day",date,24,"B");


label momr = "Momentum Portfolio";
label HDATE1= "First Holding Date";
label HDATE2= "Last Holding Date";
format HDATE1 HDATE2 HDATE3 HDATE4 HDATE5 HDATE6  HDATE7 HDATE8 
HDATE9 HDATE10 HDATE11 HDATE12 HDATE13 HDATE14 HDATE15 HDATE16 
HDATE17 HDATE18 HDATE19 HDATE20 HDATE21 HDATE22 HDATE23 HDATE24 
 is8601dt.;
rename date=form_date;
run;
 
proc sort data=umd3 nodupkey; by symbol_id form_date; run;
 
/* Portfolio returns are average monthly returns rebalanced monthly */
/*For DGTW, use ret_daily_new --> ret_diff_excess_return*/ 
proc sql;
create table umd3_1 as select
a.symbol_id,  a.form_date, a.momr, a.holding_ret_daily, b.ret_daily as ret_holding, b.date
from umd3 as a
left join test3 as b
on  a.symbol_id=b.symbol_id and a.HDATE1=b.date  ;
run; 
proc sql;
create table umd3_2 as select
a.symbol_id,  a.form_date, a.momr, b.ret_daily as ret_holding, b.date
from umd3 as a
left join test3 as b
on  a.symbol_id=b.symbol_id and a.HDATE2=b.date  ;
run; 
proc sql;
create table umd3_3 as select
a.symbol_id,  a.form_date, a.momr, b.ret_daily as ret_holding, b.date
from umd3 as a
left join test3 as b
on  a.symbol_id=b.symbol_id and a.HDATE3=b.date  ;
run;
proc sql;
create table umd3_4 as select
a.symbol_id,  a.form_date, a.momr, b.ret_daily as ret_holding, b.date
from umd3 as a
left join test3 as b
on  a.symbol_id=b.symbol_id and a.HDATE4=b.date  ;
run; 
proc sql;
create table umd3_5 as select
a.symbol_id,  a.form_date, a.momr, b.ret_daily as ret_holding, b.date
from umd3 as a
left join test3 as b
on  a.symbol_id=b.symbol_id and a.HDATE5=b.date  ;
run; 
proc sql;
create table umd3_6 as select
a.symbol_id,  a.form_date, a.momr, b.ret_daily as ret_holding, b.date
from umd3 as a
left join test3 as b
on  a.symbol_id=b.symbol_id and a.HDATE6=b.date  ;
run; 
proc sql;
create table umd3_7 as select
a.symbol_id,  a.form_date, a.momr, b.ret_daily as ret_holding, b.date
from umd3 as a
left join test3 as b
on  a.symbol_id=b.symbol_id and a.HDATE7=b.date  ;
run; 
proc sql;
create table umd3_8 as select
a.symbol_id,  a.form_date, a.momr, b.ret_daily as ret_holding, b.date
from umd3 as a
left join test3 as b
on  a.symbol_id=b.symbol_id and a.HDATE8=b.date  ;
run; 
proc sql;
create table umd3_9 as select
a.symbol_id,  a.form_date, a.momr, b.ret_daily as ret_holding, b.date
from umd3 as a
left join test3 as b
on  a.symbol_id=b.symbol_id and a.HDATE9=b.date  ;
run; 
proc sql;
create table umd3_10 as select
a.symbol_id,  a.form_date, a.momr, b.ret_daily as ret_holding, b.date
from umd3 as a
left join test3 as b
on  a.symbol_id=b.symbol_id and a.HDATE10=b.date  ;
run; 
proc sql;
create table umd3_11 as select
a.symbol_id,  a.form_date, a.momr, b.ret_daily as ret_holding, b.date
from umd3 as a
left join test3 as b
on  a.symbol_id=b.symbol_id and a.HDATE11=b.date  ;
run; 
proc sql;
create table umd3_12 as select
a.symbol_id,  a.form_date, a.momr, b.ret_daily as ret_holding, b.date
from umd3 as a
left join test3 as b
on  a.symbol_id=b.symbol_id and a.HDATE12=b.date  ;
run; 
proc sql;
create table umd3_13 as select
a.symbol_id,  a.form_date, a.momr, b.ret_daily as ret_holding, b.date
from umd3 as a
left join test3 as b
on  a.symbol_id=b.symbol_id and a.HDATE13=b.date  ;
run; 
proc sql;
create table umd3_14 as select
a.symbol_id,  a.form_date, a.momr, b.ret_daily as ret_holding, b.date
from umd3 as a
left join test3 as b
on  a.symbol_id=b.symbol_id and a.HDATE14=b.date  ;
run; 
proc sql;
create table umd3_15 as select
a.symbol_id,  a.form_date, a.momr, b.ret_daily as ret_holding, b.date
from umd3 as a
left join test3 as b
on  a.symbol_id=b.symbol_id and a.HDATE15=b.date  ;
run; 
proc sql;
create table umd3_16 as select
a.symbol_id,  a.form_date, a.momr, b.ret_daily as ret_holding, b.date
from umd3 as a
left join test3 as b
on  a.symbol_id=b.symbol_id and a.HDATE16=b.date  ;
run; 
proc sql;
create table umd3_17 as select
a.symbol_id,  a.form_date, a.momr, b.ret_daily as ret_holding, b.date
from umd3 as a
left join test3 as b
on  a.symbol_id=b.symbol_id and a.HDATE17=b.date  ;
run; 
proc sql;
create table umd3_18 as select
a.symbol_id,  a.form_date, a.momr, b.ret_daily as ret_holding, b.date
from umd3 as a
left join test3 as b
on  a.symbol_id=b.symbol_id and a.HDATE18=b.date  ;
run; 
proc sql;
create table umd3_19 as select
a.symbol_id,  a.form_date, a.momr, b.ret_daily as ret_holding, b.date
from umd3 as a
left join test3 as b
on  a.symbol_id=b.symbol_id and a.HDATE19=b.date  ;
run; 
proc sql;
create table umd3_20 as select
a.symbol_id,  a.form_date, a.momr, b.ret_daily as ret_holding, b.date
from umd3 as a
left join test3 as b
on  a.symbol_id=b.symbol_id and a.HDATE20=b.date  ;
run; 

data umd4;
    set  umd3_1
        umd3_2
         umd3_3
		  umd3_4
		  umd3_5
		  umd3_6
		umd3_7
		 umd3_8
		umd3_9
		 umd3_10
		     umd3_11
		umd3_12
			  		  umd3_13
		 umd3_14
		  umd3_15
		     umd3_16
			  umd3_17
			  			  		umd3_18
		   umd3_19
		  umd3_20

/*for holdings till n days, use umd3_1, umd_3_2, to umd_3_n;*/

;
run; 

proc sort data=umd4 ; by date momr form_date ; run;
/* Calculate Equally-Weighted returns across portfolio stocks */
/* Every date, each MOM group has J portfolios identified by formation date */
proc means data = umd4 noprint;
  by date momr form_date;
    var ret_holding;
    output out = umd5 mean=ret_holding_mean;
run;
 
/* Create one return series per MOM group every month */
proc means data = umd5 noprint;
  by date momr;
    var ret_holding_mean;
    output out = ewretdat mean= ewret std = ewretstd;
run;


data ewretdat1; set ewretdat; 
rank=momr;
rank_id=cats('PORT',rank);
 run;

proc sort data=ewretdat1; by date rank; run;
proc transpose data=ewretdat1 out=ewretdat2 ;

by date;id rank_id;var ewret;
run;


Data gout; set ewretdat2;
holding_date = intnx("day",date, 1,"e");
format holding_Date MMDDYY10.;
run;

proc sql;
	create table gout2 as select a.*, b.cmkt, b.csmb, b.cmom, b.rf_daily
		from gout as a left join crypto_factor as b on 
		a.date=b.date_temp;
	run;
data ff4; set gout2; 

PORT1_rf=PORT1-rf_daily;
PORT2_rf=PORT2-rf_daily;
PORT3_rf=PORT3-rf_daily;
PORT4_rf=PORT4-rf_daily;
PORT5_rf=PORT5-rf_daily;

ret_diff=port5_rf-port1_rf;


run;

%let lag=8;
proc model data=ff4; parms a1 ;exogenous ret_diff ; instruments / intonly;ret_diff= a1 ; fit ret_diff / gmm kernel=(bart, %eval(&lag+1),0);ods output parameterestimates=ret_diff_Excess_ret  fitstatistics=fitresult OutputStatistics=residual;
data ret_diff_Excess_ret (keep=port estimate stderr tvalue probt df );set ret_diff_Excess_ret; port='ret_diff_3factor'; if parameter='a1'; run;
proc model data=ff4; parms a1 b1 ;instruments cmkt ;ret_diff= a1 + b1*cmkt; fit ret_diff / gmm kernel=(bart,%eval(&lag+1), 0);ods output parameterestimates=ret_diff_CAPM  fitstatistics=fitresult OutputStatistics=residual;
data ret_diff_CAPM (keep=port estimate stderr tvalue probt df );set ret_diff_CAPM; port='ret_diff_3factor'; if parameter='a1'; run;
proc model data=ff4; parms a1 b1 b2 b3 ;instruments cmkt csmb cmom;ret_diff= a1 + b1*cmkt  + b2*csmb + b3*cmom; fit ret_diff / gmm kernel=(bart, %eval(&lag+1), 0);ods output parameterestimates=ret_diff_3factor  fitstatistics=fitresult OutputStatistics=residual;
data ret_diff_3factor (keep=port estimate stderr tvalue probt df );set ret_diff_3factor; port='ret_diff_3factor'; if parameter='a1'; run;

data Final; 
set 
ret_diff_Excess_ret
 ret_diff_CAPM
ret_diff_3factor
;
run;
